package com.songlanyun.modules.schedule;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.account.service.UserIdentityAssetService;
import com.songlanyun.modules.disabilityauth.service.DisabilityAuthService;
import com.songlanyun.modules.freebaggive.service.FreeBagGiveService;
import com.songlanyun.modules.pointconfig.entity.PointConfig;
import com.songlanyun.modules.pointconfig.service.PointConfigService;
import com.songlanyun.modules.usergains.service.UserBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.net.InetAddress;

/**
 * 定时任务
 * <p>
 *
 */
@Component
@Configuration
@EnableScheduling
@Lazy(false)
@Slf4j
public class ActivationAndAirdropScheduleServiceImpl {

    @Autowired
    private UserBillService userBillService;
    @Autowired
    private PointConfigService pointConfigService;

    @Autowired
    private UserIdentityAssetService userIdentityAssetService;

    @Autowired
    private AccountService accountService;
    /**
     *
     */
    @Async
    @Scheduled(cron = "${task0h5m}")
    //@Scheduled(cron = "${task05h}")
    public void activationAndAirdropTask() {
        String ipAddress = "";
        try{
            InetAddress inetAddress = InetAddress.getLocalHost();
            // 获取IP地址字符串
            ipAddress = inetAddress.getHostAddress();
            System.out.println("IP Address: " + ipAddress);
        }catch(Exception e){
            e.printStackTrace();
        }
        if(ipAddress.equals("172.30.122.31")){  //私网ip
            PointConfig config = pointConfigService.loadConfig(true);
            if(ObjectUtil.isNotNull(config) && config.getOpenPutIn()) {
                BigDecimal allActivationUseCommissionAble= userIdentityAssetService.countAllActivationUseCommissionAble();
                userBillService.batchProcess(200, (data) -> {
                    userBillService.batchActivationAndAirdrop(allActivationUseCommissionAble,data.getData());
                });

                config.setGiveTotalPoint(MathUtils.setScaleDown(config.getGiveTotalPoint().doubleValue() + config.getEveryDayPutIntPoint().doubleValue(),8));
                pointConfigService.modify(config);
            }
        }
        //获得奖励之后，清空观看记录
        accountService.update(new UpdateWrapper<AccountEntity>().set("see_times", 0));
    }


}
